How do I read hex numbers into an unsigned int in C [Solved]
Posted
by sil3nt
on Stack Overflow
See other posts from Stack Overflow
or by sil3nt
Published on 2009-09-08T22:44:19Z
Indexed on
2010/06/09
11:22 UTC
Read the original article
Hit count: 174
I'm wanting to read hex numbers from a text file into an unsigned integer so that I can execute Machine instructions. It's just a simulation type thing that looks inside the text file and according to the values and its corresponding instruction outputs the new values in the registers.
For example, the instructions would be:
- 1RXY -> Save register R with value in memory address XY
- 2RXY -> Save register R with value XY
- BRXY -> Jump to register R if xy is this and that etc..
- ARXY -> AND register R with value at memory address XY
The text file contains something like this each in a new line. (in hexidecimal)
- 120F
- B007
- 290B
My problem is copying each individual instruction into an unsigned integer...how do I do this?
#include <stdio.h>
int main(){
FILE *f;
unsigned int num[80];
f=fopen("values.txt","r");
if (f==NULL){
printf("file doesnt exist?!");
}
int i=0;
while (fscanf(f,"%x",num[i]) != EOF){
fscanf(f,"%x",num[i]);
i++;
}
fclose(f);
printf("%x",num[0]);
}
© Stack Overflow or respective owner